#include "stdafx.h"
#include "Foxync.h"
#include "Window.h"

Window::Window(HINSTANCE instance,LPCTSTR title,Window *parent,LPARAM param){
	inst=instance;
	static ubyte b;
	if(!b){
		registerClass();
		b=1;
	}
	hwnd=CreateWindow(className(),title,WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,0,CW_USEDEFAULT,0,NULL,NULL,inst,this);
	if(!hwnd)
		throw(0);
	SetWindowLong(hwnd,DWL_USER,(LONG)this);
}

PTSTR Window::className(void){
	return L"WINDOW";
}
void Window::hide(void){
	ShowWindow(hwnd,SW_HIDE);
}
int Window::messageLoop(void){
	MSG msg;
	while(GetMessage(&msg,NULL,0,0)){
		//if(!TranslateAccelerator(msg.hwnd,accelTable,&msg)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		//}
	}
	return msg.wParam;
}
LRESULT Window::onCommand(WPARAM wparam,LPARAM lparam){
	int wmId=LOWORD(wparam),wmEvent=HIWORD(wparam);
	switch (wmId){
	case IDM_EXIT:
		DestroyWindow(hwnd);
		break;
	default:
		return DefWindowProc(hwnd,WM_COMMAND,wparam,lparam);
	}
	return 0;
}
LRESULT Window::onCreate(LPARAM lparam){
	return 0;
}
LRESULT Window::onCreate(HWND window,LPARAM lparam){
	CREATESTRUCT* ps=((CREATESTRUCT*)lparam);
	LPARAM lp=(LPARAM)ps->lpCreateParams;
	if(lp){
		SetWindowLong(window,DWL_USER,lp);
		return((Window*)lp)->onCreate(lparam);
	}
	return 0;
}
void Window::onDestroy(void){
}
LRESULT Window::onDeviceChange(WPARAM wparam,LPARAM lparam){
	return FALSE;
}
void Window::onEraseBkgnd(WPARAM wparam,LPARAM lparam){
	DefDlgProc(hwnd,WM_ERASEBKGND,wparam,lparam);
}
void Window::onMove(WPARAM id,LPARAM mouseMessage){
}
void Window::onPaint(void){
	DefWindowProc(hwnd,WM_PAINT,0,0);
	/*PAINTSTRUCT ps;
	HDC hdc=BeginPaint(hwnd,&ps);
	EndPaint(hwnd,&ps);*/
}
void Window::onSize(WPARAM id,LPARAM mouseMessage){
}
LRESULT Window::onUser(uint message,WPARAM wparam,LPARAM lparam){
	return 0;
}
void Window::registerClass(void){
	WNDCLASSEX c;
	c.cbSize		=sizeof(WNDCLASSEX);
	c.style			=CS_HREDRAW|CS_VREDRAW;
	c.lpfnWndProc	=wndProc;
	c.cbClsExtra	=0;
	c.cbWndExtra	=DLGWINDOWEXTRA;
	c.hInstance		=inst;
	c.hIcon			=LoadIcon(inst,MAKEINTRESOURCE(IDI_XYNC));
	c.hCursor		=LoadCursor(NULL,IDC_ARROW);
	c.hbrBackground	=(HBRUSH)(COLOR_WINDOW+1);
	c.lpszMenuName	=0;
	c.lpszClassName	=className();
	c.hIconSm		=LoadIcon(inst,MAKEINTRESOURCE(IDI_XYNS));
	RegisterClassEx(&c);
}
LRESULT Window::sendMessage(uint message,WPARAM wparam,LPARAM lparam){
	return SendMessage(hwnd,message,wparam,lparam);
}
void Window::setCursor(HCURSOR cursor){
	SetCursor(cursor);
}
void Window::setFocus(void){
	SetFocus(hwnd);
}
void Window::show(int command){
	ShowWindow(hwnd,command);
}
void Window::update(void){
	UpdateWindow(hwnd);
}
LRESULT CALLBACK Window::wndProc(HWND window,UINT message,WPARAM wparam,
	LPARAM lparam){
	Window *ptr=(Window*)GetWindowLong(window,DWL_USER);
	if(ptr)switch(message){
	case WM_COMMAND:
		return ptr->onCommand(wparam,lparam);
	case WM_DESTROY:
		ptr->onDestroy();
		break;
	case WM_DEVICECHANGE:
		return ptr->onDeviceChange(wparam,lparam);
	case WM_ERASEBKGND:
		ptr->onEraseBkgnd(wparam,lparam);
		break;
	case WM_MOVE:
		ptr->onMove(wparam,lparam);
		break;
	case WM_PAINT:
		ptr->onPaint();
		break;
	case WM_SIZE:
		ptr->onMove(wparam,lparam);
		break;
	default:
		if(message>=WM_USER)
			return ptr->onUser(message,wparam,lparam);
		else return DefWindowProc(window,message,wparam,lparam);
	}else switch(message){
	case WM_CREATE:
		return onCreate(window,lparam);
	case WM_INITDIALOG:
		return DefDlgProc(window,message,wparam,lparam);
	default:
		return DefWindowProc(window,message,wparam,lparam);
	}
	return 0;
}
